package com.qfedu.biz.impl;

import com.qfedu.biz.ShopCarBiz;
import com.qfedu.entity.GoodsEntity;
import com.qfedu.entity.ShopCarEntity;
import com.qfedu.mapper.ShopCarPojoMapper;
import com.qfedu.pojo.GoodsPojo;
import com.qfedu.pojo.ShopCarPojo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class ShopCarBizimpl implements ShopCarBiz {
    @Autowired
    private ShopCarPojoMapper shopCarPojoMapper;

    //查找购物车
    @Override
    public ShopCarEntity queryShopCar(int cid) {
        ShopCarEntity shopCarEntity = shopCarPojoMapper.queryShopCar(cid);

        return shopCarEntity;
    }


    //添加购物车
    @Override
    public Boolean insertShopCar(ShopCarEntity shopCarEntity) {
        Boolean aBoolean = shopCarPojoMapper.insertShopCar(entityToPojo(shopCarEntity));
        return aBoolean;
    }
   //添加购物车商品
    @Override
    public Boolean insertShopcarAndGoods(Map<String,Object> map) {
        Boolean aBoolean = shopCarPojoMapper.insertShopcarAndGoods(map);
        return aBoolean;
    }
//删除当前购物车所有商品
    @Override
    public Boolean deleteByShopid(int scid) {
        Boolean aBoolean = shopCarPojoMapper.deleteByShopid(scid);
        return aBoolean;
    }
//更新购物车商品数量
    @Override
    public Boolean updateShopcar(Map<String, Object> map) {
        Boolean aBoolean = shopCarPojoMapper.updateShopcar(map);
        return aBoolean;
    }

    @Override
    public Boolean deleteGoods(String gid, int scid) {
        Boolean aBoolean = shopCarPojoMapper.deleteGoods(gid, scid);
        return aBoolean;
    }


    //pojo转换成Entity
    private ShopCarEntity pojoToEntity(ShopCarPojo shopCarPojo){
        if (shopCarPojo==null){
            return null;
        }
        ShopCarEntity shopCarEntity = new ShopCarEntity(shopCarPojo.getCid(), shopCarPojo.getCid(), shopCarPojo.getScname());
        if (shopCarPojo.getGoodid()!=null){
            shopCarEntity.setGoodid(shopCarPojo.getGoodid());
        }
        return shopCarEntity;
    }

    //entity转化成Pojo
    private ShopCarPojo entityToPojo(ShopCarEntity shopCarEntity){
        if (shopCarEntity==null){
            return null;
        }
        ShopCarPojo shopCarPojo = new ShopCarPojo(shopCarEntity.getCid(), shopCarEntity.getCid(), shopCarEntity.getScname());
        if (shopCarEntity.getGoodid()!=null){
            shopCarPojo.setGoodid(shopCarEntity.getGoodid());
        }
        return shopCarPojo;
    }
}
